<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function curl_get($url,&$httpCode = 0){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    //不做证书校验，部署在linux环境下请改位true
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    $file_contents = curl_exec($ch);
    $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $file_contents;
}

function curl_post_raw($url, $rawData)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);
    curl_setopt(
        $ch, CURLOPT_HTTPHEADER,
        array(
            'Content-Type: text'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}

/**
 * @param string $url post请求地址
 * @param array $params
 * @return mixed
 */
function curl_post($url, $params = array())
{
    if (empty($params)){
        $data_string = "{}";
    }else{
        $data_string = json_encode($params);
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt(
        $ch, CURLOPT_HTTPHEADER,
        array(
            'content-type:application/json'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}

/**
 * @param string $url post请求地址
 * @param array $params
 * @return mixed
 */
function curl_post_gbk($url, array $params = array())
{
    $data_string = json_encode($params,JSON_UNESCAPED_UNICODE);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt(
        $ch, CURLOPT_HTTPHEADER,
        array(
            'content-type:application/json'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}
/**
 * 随机字符串
 * @param $length
 * @return null|string
 */
function getRandChar($length){
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol)-1;

    for ($i=0;$i<$length;$i++){
        $str .= $strPol[rand(0,$max)];
    }
    return $str;
}


/**
 * 将XML转为array
 * @param $xml
 * @return mixed
 */
function Xml2Array($xml)
{
    //禁止引用外部xml实体
    libxml_disable_entity_loader(true);//重要
    $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    return $values;
}

/**
 * 时间差转换
 * @param $time
 * @return float
 */
function getTimeDifference($time){
    if (floor($time/86400) != 0){//天
        return floor($time/86400).'天前';
    }
    if (floor($time/3600) != 0){//小时
        return floor($time/3600).'小时前';
    }
    if (floor($time/60) != 0){//分钟
        return floor($time/60).'分钟前';
    }
    return '刚刚';
}


/**
 * 检验数据的真实性，并且获取解密后的明文.
 * @param $encryptedData string 加密的用户数据
 * @param $iv string 与用户数据一同返回的初始向量
 * @param $data string 解密后的原文
 *
 * @return int 成功0，失败返回对应的错误码
 */
 function decryptData( $appid,$sessionKey,$encryptedData, $iv, &$data )
{
    if (strlen($sessionKey) != 24) {
        return -41001;
    }
    $aesKey=base64_decode($sessionKey);
    if (strlen($iv) != 24) {
        return -41002;
    }
    $aesIV=base64_decode($iv);
    $aesCipher=base64_decode($encryptedData);
    $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
    $dataObj=json_decode( $result );
    if( $dataObj  == NULL )
    {
        return -41003;
    }
    if( $dataObj->watermark->appid != $appid )
    {
        return -41004;
    }
    $data = $result;
    return $data;
}

/**
 * 获取微信jssdk所需签名
 * @param $noncestr
 * @param $jsapi_ticket
 * @param $timestamp
 * @param $url
 * @return string
 */
function getWxJsapiTicket($noncestr,$jsapi_ticket,$timestamp,$url){
    $string1 = 'jsapi_ticket='.$jsapi_ticket.'&noncestr='.$noncestr.'&timestamp='.$timestamp.'&url='.$url;
    return sha1($string1);
}

/**
 * 日志记录
 * @param $name
 * @param $time
 * @param $data
 * @return bool|int
 */
function insertLog($name,$time,$data){
    $content = "#time->".date('Y-m-d H;i:s',$time).json_encode($data).PHP_EOL;
    return file_put_contents($name,$content,FILE_APPEND);
}

